//
//  LeonShopViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/25.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit
import SVProgressHUD
class LeonShopViewController: BBBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin {
            visitorView?.setupVisitorInfo(animationType: 3, imageName: "Visitor_shop_back", message: "Go sign in. Choose a favorite product!")
            return
        }
        setNav()
        self.view.addSubview(webView)
        webView.delegate = self
        // 连接加载
        webView.loadRequest(URLRequest.init(url: URL.init(string: "https://leoncycle.de/shop3/")!))
        /**
         让网页自动使用大小
         */
        webView.sizeToFit()
        webView.sizeThatFits(self.view.bounds.size)
        
        /**
         网页收缩适配
         */
        webView.scalesPageToFit = true
    }
    
    private lazy var webView:UIWebView = {
        var type = BBUtils.getPhoneType()
        var frame = CGRect.zero
        if type == PhoneType.PHONE_X || type == PhoneType.PHONE_MAX_XR{
             frame = CGRect(x: 0, y: 90, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height-170)
        }else{
             frame = CGRect(x: 0, y: 60, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height-100)
        }
        var web = UIWebView.init(frame: frame)
        return web
    }()

    private lazy var activity:UIActivityIndicatorView = {
        let activity = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.gray)
        return activity
    }()
    
    private func setNav() {
        let rightItem = UIBarButtonItem.init(customView: activity)
        self.navigationItem.rightBarButtonItem = rightItem
        activity.startAnimating()
    }
}

extension LeonShopViewController:UIWebViewDelegate{

    func webViewDidStartLoad(_ webView: UIWebView) {
       activity.startAnimating()
        print("webViewDidStartLoad")
    }
    // MARK: 网页加载失败的时候调用
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        print("didFailLoadWithError")
       activity.stopAnimating()
    }
    // MARK: 网页加载完成时候调用
    func webViewDidFinishLoad(_ webView: UIWebView) {
        print("webViewDidFinishLoad")
         activity.stopAnimating()
    }
    // TODO: 是网页发起请求前,询问是否可以发起请求
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
        return true
    }
}
